package kr.hkit.androidcomplete03.ch20;

import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class FragmentTest extends Activity {
	public final static String TAG = "FragmentCycle";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.d(TAG, "Activity - onCreate()");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch20_fragmenttest);
	}
	

	@Override
	protected void onStart() {
		Log.d(TAG, "Activity - onStart()");
		super.onStart();
	}

	@Override
	protected void onResume() {
		Log.d(TAG, "Activity - onResume()");
		super.onResume();
	}

	@Override
	protected void onPause() {
		Log.d(TAG, "Activity - onPause()");
		super.onPause();
	}

	@Override
	protected void onStop() {
		Log.d(TAG, "Activity - onStop()");
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		Log.d(TAG, "Activity - onDestroy()");
		super.onDestroy();
	}

	public static class CounterFragment extends Fragment {
		TextView textCounter;
		@Override
		public void onAttach(Activity activity) {
			Log.d(TAG, "          Fragment - onAttach()");
			super.onAttach(activity);
		}

		@Override
		public void onCreate(Bundle savedInstanceState) {
			Log.d(TAG, "          Fragment - onCreate()");
			super.onCreate(savedInstanceState);
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			Log.d(TAG, "          Fragment - onCreateView()");
			
			View root = inflater.inflate(R.layout.ch20_counterfragment, container, false);
			Button btnIncrease = (Button) root.findViewById(R.id.ch20_btnincrease);
			textCounter = (TextView) root.findViewById(R.id.ch20_txtcounter);
			
			if(savedInstanceState != null){
				textCounter.setText(Integer.toString(savedInstanceState.getInt("count")));
			}

			//int start = 0;
			Bundle bundle = getArguments();
			if(bundle != null){
				textCounter.setText(Integer.toString(bundle.getInt("start")));
			}
			
			btnIncrease.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					int cnt = Integer.parseInt(textCounter.getText().toString());
					textCounter.setText(Integer.toString(cnt + 1));
					
				}
			});
			return root;
		}

		@Override
		public void onSaveInstanceState(Bundle outState) {
			super.onSaveInstanceState(outState);
			int a = Integer.parseInt(textCounter.getText().toString());
			outState.putInt("count", a);
		}
		
		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			Log.d(TAG, "          Fragment - onActivityCreated()");
			super.onActivityCreated(savedInstanceState);
		}

		@Override
		public void onStart() {
			Log.d(TAG, "          Fragment - onStart()");
			super.onStart();
		}

		@Override
		public void onResume() {
			Log.d(TAG, "          Fragment - onResume()");
			super.onResume();
		}

		@Override
		public void onPause() {
			Log.d(TAG, "          Fragment - onPause()");
			super.onPause();
		}

		@Override
		public void onStop() {
			Log.d(TAG, "          Fragment - onStop()");
			super.onStop();
		}

		@Override
		public void onDestroyView() {
			Log.d(TAG, "          Fragment - onDestroyView()");
			super.onDestroyView();
		}

		@Override
		public void onDestroy() {
			Log.d(TAG, "          Fragment - onDestroy()");
			super.onDestroy();
		}

		@Override
		public void onDetach() {
			Log.d(TAG, "          Fragment - onDetach()");
			super.onDetach();
		}
		
	}
}
